<?php
/**
 * Created by Dumitru Russu
 * User: developer
 * Date: 11/5/12
 * Time: 9:37 AM
 * To change this template use File | Settings | File Templates.
 */
class Page_Be_Payments_PaymentMethods extends Page {
	/**
	 * @var Db_Payments_PaymentMethods
	 */

	protected $tableObject;

	function __construct() {
		$this->idTableObject = get('id', 'int');
		$this->classTableObject = 'Db_Payments_PaymentMethods';
		$this->actionsTableObject = array(
			'addEditAction',
			'saveAction'
		);
		parent::__construct();
	}

	public function defaultAction() {
		$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit');
		self::$TPL->assign('filePath', self::$PROJECT['pr_resources_live']);

		$paymentMethods = Db_Payments_PaymentMethods::findAll();
		self::$TPL->assign('paymentMethods', $paymentMethods);

		self::$TPL->setBodyTemplate('list_payment_methods');
		return array(
			'type' => 'display'
		);
	}

	public function addEditAction() {
		perm('PAYMENT_METHODS_EDIT', self::$PAGE['p_url']);
		if(!empty($this->tableObject->objectArray->id)) {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'edit') . ' "' . t($this->tableObject->objectArray->pm_module, $this->tableObject->objectArray->pm_name) . '"', '?action=addEdit&id=' . $this->tableObject->objectArray->id, true);
			self::$TPL->assign('paymentMethod', $this->tableObject->objectArray);
			self::$TPL->assign('filePath', self::$PROJECT['pr_resources_live']);
		} else {
			$this->addBreadcrumb(t(T_FORM_BUTTON, 'add'), '?action=addEdit', true);
		}

		self::$TPL->setBodyTemplate('add_edit_payment_methods');
		return array(
			'type' => 'display'
		);
	}

	public function saveAction() {
		perm('PAYMENT_METHODS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');
		$this->tableObject->pm_name = post('pm_name', 'str');
		$this->tableObject->pm_description = post('pm_description', 'str');

		if(!empty($this->tableObject->pm_name) && !empty($this->tableObject->pm_description)) {

			if(!$this->tableObject->save(true)) {
				$this->error = true;
				$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
			}


			if (isset($_FILES['pm_image'])) {
				if ($_FILES['pm_image']["error"] == 0) {

					$extensions = conf('ALLOWED_EXTENSIONS');
					$result = Utils::uploadFile($_FILES['pm_image'], PAYMENTS_PHOTOS_PAYMENT_METHODS_PATH, $this->tableObject->id, $extensions['users'], false, 0, false, 0);

					$this->tableObject->pm_image = $result['data']['path'];
					if(!$this->tableObject->save(true)) {
						$this->error = true;
						$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
					}
					if(!$result['error']) {
						$oldPath = PUBLIC_RESOURCES_PATH . $this->tableObject->objectArray->pm_image;
						@unlink($oldPath);
					}
					if ($result['error'] && !isset($result['data']['path'])) {
						$this->error = true;
						if (isset($result['message']) && strlen($message=$result['message'])) {
							$this->message = t(T_FORM_ERROR, $result['message']);
						} else {
							$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
						}
					}
				}

			}
		}
		return array(
			'type' => 'redirect',
			'query' => false
		);
	}

	public function deleteAction() {
		perm('PAYMENT_METHODS_EDIT', self::$PAGE['p_url']);
		$this->message = t(T_FORM_MSG, 'success');

		if(!Db_Payments_PaymentMethods::abstractDelete($this->idTableObject)) {
			$this->error = true;
			$this->message = t(T_FORM_ERROR, 'unknown_error_occurred');
		}

		return array(
			'type' => 'redirect',
			'query' => false
		);
	}
}
